<?php

namespace app\traveller\controller;
use think\Controller;
use think\Validate;
use think\Db;

class OrderTraveller extends  Controller{
    /**
     * @var array 黑名单方法，禁止访问某些方法
     */
//    protected static $allowList = ['index'];
    public function index() {
        $map['o.order_id']=input('id');
        $order=Db::name('order')
            ->alias('o')
            ->join('tp_route r','r.id=o.route_id','left')
            ->where($map)
            ->field("o.*,r.title as r_title")
            ->find();
        $this->assign("order",$order);
        return $this->fetch();
    }
    public function traveller() {
        $map['o.order_id']=input('id');
        $order=Db::name('order')
            ->alias('o')
            ->join('tp_route r','r.id=o.route_id','left')
            ->where($map)
            ->field("o.*,r.title as r_title")
            ->find();
        if(!$this->request->isAjax()){

            $this->assign("order",$order);
            $travellers=Db::name("order_traveler")->where('oid',$order['id'])->select();
            $this->assign("travellers",$travellers);
            return $this->fetch();
        }else{
            if($order['isallot']||$order['order_status']=="close"||$order['order_status']=="finish"){
                return ajax_return_error("订单已分配或已结束");
            }
            $input=input();
            $up_traveller_id[]=0;
            if(!$input['travellers']){
                $this->error("不可为空");
            }
            //更改
            foreach ($input['travellers'] as $traveller){
                if($traveller['id']){
                    Db::name('order_traveler')->update($traveller);
                    $up_traveller_id[]=$traveller['id'];
//                    dump(Db::name('order_traveler')->getLastSql());
                }
            }

            //删除
            if($up_traveller_id){
                $del_map['oid']=$order['id'];
                $del_map['id']=array('not in',$up_traveller_id);
                Db::name("order_traveler")->where($del_map)->delete();
            }
            //新增
            foreach ($input['travellers'] as $traveller){
                if(!$traveller['id']){
                    $traveller['oid']=$order['id'];
                    Db::name('order_traveler')->insert($traveller);
//                    dump(Db::name('order_traveler')->getLastSql());
                }
            }
            // 信息收集成功后,修改订单处理状态
            Db::name('order')
                ->where('order_id',$input['id'])
                ->setField('dispose_status','vehicle_dispatching');
            $this->success("提交成功");
        }
    }

}
